﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_ZhanQuBiaoService : It_ZhanQuBiaoService
    {
        public readonly MyContext _Context;
        public t_ZhanQuBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        public int Add(t_ZhanQuBiao t_ZhanQuBiao)
        {
            _Context.t_ZhanQuBiao.Add(t_ZhanQuBiao);
            return _Context.SaveChanges();
        }

        public List<t_BuMenBiao> GetBuMenList()
        {
            return _Context.t_BuMenBiao.Where(x => x.ZhanQuZhuangTai == "战区部门" && x.ZhuangTai == "启用").ToList();
        }

        public List<t_ZhanQuBiao> GetList()
        {
            return _Context.t_ZhanQuBiao.Include(x => x.t_ZhanDuiBiaos).ToList();
        }
        public t_ZhanQuBiao GetId(int id)
        {
            return _Context.t_ZhanQuBiao.FirstOrDefault(x => x.Id == id);
        }

  

        public string ReturnWhere(t_ZhanQuBiao t_ZhanQuBiao)
        {
            string returnstr = "Id != 1";
            if (t_ZhanQuBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_ZhanQuBiao.BeiZhu1 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_ZhanQuBiao.BeiZhu2 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_ZhanQuBiao.BeiZhu3 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_ZhanQuBiao.BeiZhu4 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_ZhanQuBiao.BeiZhu5 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_ZhanQuBiao.BeiZhu6 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_ZhanQuBiao.BeiZhu7 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_ZhanQuBiao.BeiZhu8 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_ZhanQuBiao.BeiZhu9 + ")";
            }
            if (t_ZhanQuBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_ZhanQuBiao.BeiZhu10 + ")";
            }
            if (t_ZhanQuBiao.ZhanQuMingCheng != null)
            {
                returnstr = returnstr + " and ZhanQuMingCheng.Contains(" + t_ZhanQuBiao.ZhanQuMingCheng + ")";
            }
            if (t_ZhanQuBiao.ZhuangTai != null)
            {
                returnstr = returnstr + " and ZhuangTai.Contains(" + t_ZhanQuBiao.ZhuangTai + ")";
            }
            return returnstr;
        }

        public t_ZhanQuBiao GetZhanQuId(int id)
        {
            return _Context.t_ZhanQuBiao.Where(x => x.Id == id).ToList()[0];
        }

        public string GetListWithFenYe(t_ZhanQuBiao t_ZhanQuBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_ZhanQuBiao);
            var list = new List<t_ZhanQuBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_ZhanQuBiao.Include(x => x.t_BuMenBiao).Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_ZhanQuBiao.Include(x => x.t_BuMenBiao).ToList();
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
